package com.assistant.data;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import com.assistant.tools.Constants;
import com.assistant.tools.PictureFileMgr;

public class FileMgr  {

	private String folderName;
	private Queue<String> fileNames;
	private int max_num;
	private static FileMgr fm=null;
	
	private FileMgr(Context ctx,String folderName,int max){
		if(folderName!=null)
			this.folderName=ctx.getCacheDir()+folderName;
		else
			this.folderName=ctx.getCacheDir()+Constants.FILE_FOLDER;
		fileNames=new LinkedList<String>();
		this.max_num=max;
		synData();
	}

	public static synchronized FileMgr getInstance(Context ctx,String folderName,int max){
		if(fm==null)
			fm=new FileMgr(ctx,folderName,max);
		return fm;
	}
	public boolean add(String filename,Bitmap bm){
		 boolean deleteEar=true;
		 if(!fileNames.contains(filename)){
			 if(bm!=null)
			 {
				 if(fileNames.size()>=max_num){
					 deleteEar=deleteEarlist();
				 }
				 if(deleteEar){
					 try {
					 	PictureFileMgr.saveFile(bm, folderName, filename);
					 	fileNames.offer(filename);
					 	return true;
					 } catch (IOException e) {
				// TODO Auto-generated catch block
						 Log.i("Picture file save exception:", e.getMessage());
						 return false;
					 }
				 }
			 }
		 }
		return false;
	}
	
	public boolean delete(String filename){
    	if(fileNames.contains(filename)){
	    	try{
	    		boolean result=PictureFileMgr.delete(folderName+filename);
	    		if(result==true)
	    			fileNames.remove(filename);
	    		return result;
	    	}catch(Exception e)
	    	{
	    		Log.i("Picture file delete exception:", e.getMessage());
	    		return false;
	    	}
    	}
		return false;
	}
	
	public Bitmap get(String filename){
		Bitmap pic=null;
		if(fileNames.contains(filename)){
			 pic=PictureFileMgr.getFile(folderName+filename);
		}
		return pic;
	}
	
	public boolean clear(){
		return false;
	}
	
	public boolean update(String filename,Bitmap bm){
		if(fileNames.contains(filename)&&bm!=null)
		{
			try {
				PictureFileMgr.saveFile(bm, folderName, filename);
				fileNames.remove(filename);
				fileNames.offer(filename);
				return true;
				 } catch (IOException e) {
			// TODO Auto-generated catch block
					 Log.i("Picture file save exception:", e.getMessage());
					 return false;
			}
			
		}
		return false;
	}
	public boolean deleteEarlist(){
		 if(!fileNames.isEmpty()){
			 String path=fileNames.poll();
			 if(path!=null){
				 try{
					 return PictureFileMgr.delete(folderName+path);
				 }catch(Exception e)
				 {
					 Log.i("Picture file delete exception:", e.getMessage());
					 return false;
				 }
			 }
		 }
		 return false;
	 }
	private void synData(){
		if(PictureFileMgr.createFolder(folderName)){
			String filenames[]=PictureFileMgr.getAllFilesNames(folderName);
			fileNames.clear();
			for(int i=0;i<filenames.length;i++)
				fileNames.offer(filenames[i]);
		}
	}

}
